﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aoxuelf.MDSTool.Model;

namespace Aoxuelf.MDSTool.SQL
{
    public class BLLUpdateMethodWriter : MethodWriter
    {
        public BLLUpdateMethodWriter(BLLUpdateMethodInfo methodWriteInfo)
            : base(methodWriteInfo)
        {
        }

        protected override void WriteSQLParams()
        {
            SQLParamInfo p1 = new SQLParamInfo { Name = "#DLR_ID#", Text = "专营店ID", Type = "string" };
            SQLParamInfo p2 = new SQLParamInfo { Name = "#DLR_CODE#", Text = "专营店编码", Type = "string" };
            SQLParamInfo p3 = new SQLParamInfo { Name = "#USER_ID#", Text = "当前登录人ID", Type = "string" };
            WriteSQLParam(p1);
            WriteSQLParam(p2);
            WriteSQLParam(p3);
        }

        protected override void WriteMethodSig()
        {
            string format = "public IDictionary<string, object> {0}(IDictionary<string, object> dicParam)";
            builder.AppendLine(string.Format(format, methodWriteInfo.MethodName));
        }

        protected override void WriteMethodBody()
        {
            builder.AppendLine("IDictionary<string, object> dicRet = new Dictionary<string, object>();");
            builder.AppendLine("DbTransaction tran = Gateway.Default.BeginTransaction();");
            builder.AppendLine("");
            builder.AppendLine("try");
            builder.AppendLine("{");
            builder.AppendLine(string.Format("    // dal.{0}(tran, dicParam);", methodWriteInfo.MethodName));
            builder.AppendLine("    tran.Commit();");
            builder.AppendLine("    dicRet = ExecutionResult.Success(\"保存成功！\");");
            builder.AppendLine("}");
            builder.AppendLine("catch (Exception ex)");
            builder.AppendLine("{");
            builder.AppendLine("    if (tran != null)");
            builder.AppendLine("    {");
            builder.AppendLine("        tran.Rollback();");
            builder.AppendLine("    }");
            builder.AppendLine("    dicRet = ExceptionHelper.ExceptioinManage(ex, dicRet);");
            builder.AppendLine("    return dicRet;");
            builder.AppendLine("}");
            builder.AppendLine("finally");
            builder.AppendLine("{");
            builder.AppendLine("    if (tran != null)");
            builder.AppendLine("    {");
            builder.AppendLine("        tran.Dispose();");
            builder.AppendLine("    }");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("return dicRet;");
        }

        protected override void WriteReturnBody()
        {
            WriteContent(ReturnContent.Exe_Flag);
            WriteContent(ReturnContent.Exe_UserMsg);            
        }
    }
}
